/**
 * Created by Churcy on 2017/1/10.
 */
window.onload = function() {
	Outer.searchOuter();
}

var Outer = {

	searchOuter : function() {
		$("#searchBTN").on('click', function() {
			var conditions = getCurrentConditions();
			conditions.pageNo = 1;
			ajaxSearchOuterList(conditions);
		});
		$("#resetBTN").on('click', function() {
			resetFn();
		});
	}
}
function searchFn() {
	var conditions = getCurrentConditions();
	console.log("searchFN")
	var response = ajaxSearchOuterList(conditions);
}
function resetFn() {

	$("#regCode").val("");
	$("#code").val("");
	$("#lineName").val("");

}

function getCurrentConditions() {
	console.log("getCurrentConditions");
	var regCode = $("#regCode").val();
	var code = $("#code").val();
	var lineName = $("#lineName").val();
	var pageSize = $("#n").val();
	var pageNo = $("#p").val();
	var appKey = $("#APP_KEY").val();

	console.log("val取值:" + status);
	var conditions = {
		regCode : encodeURI(regCode),
		lineName : encodeURI(lineName),
		code : code,
		pageIndex : pageNo,
		appKey : appKey
	};
	return conditions;

}
function ajaxSearchOuterList(conditions) {
	console.log("ajaxSearchOuterList");
	$.ajax({
		type : "GET",
		url : PROJECT_CTX + "/trafficback/appListOuter",
		data : conditions,
		dataType : "json",
		success : function(data) {
			console.log(data)
			var result = eval(data);
			var resultCode = result.status.code;
			console.log(resultCode)
			console.log(result.line);
			if (1 == resultCode) {
				// 清空当前数据
				var outerList = $("#outerList").empty();
				// 填写查询出来的数据
				$.each(result.line, function(i, item) {
					var tr = "<tr>";

					tr += "<td >";
					tr += item.regCode;
					tr += "</td>"

					tr += "<td >";
					tr += item.code;
					tr += "</td>"

					tr += "<td >";
					tr += item.lineName;
					tr += "</td>"

					tr += "<td >";
					tr += item.createTime;
					tr += "</td>"

					tr += "<td >";
					tr += " <a class='table-link primary' title='修改' href='"+ PROJECT_CTX + "/traffic/app/outer/info?dataid="+ item.dataid + "&appKey=" + conditions.appKey+ "'>修改</a>";
					tr += " <a class='table-link primary' title='修改' href='"+ PROJECT_CTX + "/traffic/app/outer/modify?dataid="+ item.dataid + "&appKey=" + conditions.appKey+ "&status=3'>删除</a>";
					tr += "</td >";

					tr += "</tr>";
					outerList.append(tr);
					// 更新分页栏
					reloadPage(result);
				});
			}

		}

		,
		error : function(XMLHttpRequest, textStatus, errorThrown) {
			console.log(XMLHttpRequest.status);
			console.log(XMLHttpRequest.readyState);
			console.log(textStatus);
		}
	});
}

$("#normal").on('click', function() {
	$("#normal").removeClass().addClass("btn btn-primary");
	$("#unable").removeClass().addClass("btn btn-default");

});
$("#unable").on('click', function() {
	$("#normal").removeClass().addClass("btn btn-default");
	$("#unable").removeClass().addClass("btn btn-primary");

});

function deleteApp(dataid,appKey){
	swal({
		  title: "确定删除么?",
		  text: "删除后您将无法恢复,如需恢复请联系管理员!",
		  type: "warning",
		  showCancelButton: true,
		  confirmButtonColor: "#DD6B55",
		  confirmButtonText: "确定, 删除!",
		  closeOnConfirm: false,
		  showLoaderOnConfirm: true,
		},
		function(){
			setTimeout(function(){
				var conditions = {
						dataid :dataid,
						appKey :appKey,
						status :3
				}
				$.ajax({
					type:"GET",
					data:conditions,
					url:PROJECT_CTX + "/traffic/app/outer/modify",
					dataType:"json",
					success:function(data){
						alert(1)
					}
				})
				swal({
					title: "已删除",
					text: "未录入设备信息已删除",
					type: "success"},
				function(){
					window.location.href = PROJECT_CTX+ "/traffic/app/outer?appKey=" + appKey;
				});
				
			  }, 2000);
		});
		
}
function pageChange(dataid,appKey){
	
	
}